package cn.com.server.task;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import cn.com.server.service.InfoService;
import cn.com.server.task.QuartzJob;

@Service
public class AutoStartSystemInitServerImp implements InitializingBean{
	
	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;
	
	@Autowired
	private InfoService InfoService;

	@Override
	public void afterPropertiesSet() throws Exception {
		boolean ff = false;
		if(ff) {
			System.out.println("加载任务......");
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class).withIdentity("JobName=sysInfo-jn","JobGroupName=sysInfo-jgn").storeDurably().build();
			jobDetail.getJobDataMap().put("InfoService", InfoService);
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?"); 
			CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("TriggerName=sysInfo-tn","TriggerGroupName=sysInfo-tgn").withSchedule(scheduleBuilder).build();  
			scheduler.scheduleJob(jobDetail, trigger);
			System.out.println("加载任务完成");
		}
	}

}
